/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import biz.LoginProgess;
import da.exceptions.PreexistingEntityException;
import da.exceptions.RollbackFailureException;
import entities.Account;
import entities.News;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author THOHCGC00177
 */
@ManagedBean
@SessionScoped
public class NewsProgess {

    @EJB
    private LoginProgess loginProgess;
    @EJB
    private biz.NewsProgess newsProgess;
    private List<News> listNews;
    private String title;
    private String content;
    private String msg;
    private boolean check;
    private News newsItem;
    private List<News> listAllNews;
    private News newNews;

    /**
     * Function create List news
     * 
     */
    public void createListNews() {
        //call ejb to get List News
        listNews = newsProgess.getNewsForIndex();
    }

    public List<News> getListNews() {
        return listNews;
    }

    /**
     * check clear news
     */
    public void checkClear() {
        boolean temp = check;
        if (temp == true) {
            check = false;
            clear();
        }
    }
    /**
     * reset all text
     */
    public void clear() {
        title = null;
        content = null;
        msg = null;
    }

    public void createListAllNews() {
        listAllNews = newsProgess.getAllNews();
    }

    public String doCreateNews() {
        clear();
        //return to create_news.xhtml
        return "create_news.xhtml";
    }

    /**
     *
     *
     * Function show  new detail
     *
     * @param temp
     * @return
     */
    public String showNewsDetail(News temp) {
        newsItem = newsProgess.getNewsById(temp.getId());
        return "news_detail";
    }
    
     public String showNewsDetailAdmin(News temp) {
        newsItem = newsProgess.getNewsById(temp.getId());
        return "new_detail_admin";
    }
      public String showNewsDetailEmp(News temp) {
        newsItem = newsProgess.getNewsById(temp.getId());
        return "new_detail_employee";
    }
       public String showNewsDetailTech(News temp) {
        newsItem = newsProgess.getNewsById(temp.getId());
        return "new_detail_technician";
    }
       /**
        * Function create news processing
        * @param name
        * @return 
        */
    public String createNews(String name) {
        //call ejb to get username form DB
        Account user = loginProgess.getUser(name);
        Date date = new Date();
        //Validate textbox
        if ("".equals(title)) {
            msg = "Insert title please!";
            return "";
        }
        if ("".equals(content)) {
            msg = "Insert content please!";
            return "";
        }
        //Create new News
        try {
            News news = new News(1);
            news.setId(1);
            news.setTitle(title);
            news.setContent(content);
            news.setAcountId(user);
            news.setDate(date);
            //call ejb to create News
            newsProgess.createNews(news);
            newNews = news;
            check = true;
        } catch (PreexistingEntityException ex) {
            Logger.getLogger(NewsProgess.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RollbackFailureException ex) {
            Logger.getLogger(NewsProgess.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(NewsProgess.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "new_news.xhtml";
    }

    public List<News> getListAllNews() {
        return listAllNews;
    }

    public News getNewNews() {
        return newNews;
    }

    public void setNewNews(News newNews) {
        this.newNews = newNews;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public News getNewsItem() {
        return newsItem;
    }

    public NewsProgess() {
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}
